package com.jzo2o.customer.service.impl;

import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.common.constants.CommonStatusConstants;
import com.jzo2o.common.constants.ErrorInfo;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.InstitutionRegisterReqDTO;
import com.jzo2o.customer.model.dto.response.LoginResDTO;
import com.jzo2o.customer.service.IInstitutionRegisterService;
import com.jzo2o.customer.service.IServeProviderService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class IInstitutionRegisterServiceImpl implements IInstitutionRegisterService {
    @Resource
    private SmsCodeApi smsCodeApi;
    @Resource
    private IServeProviderService serveProviderService;
    @Resource
    PasswordEncoder passwordEncoder;
    @Override
    public void institutionRegister(InstitutionRegisterReqDTO registerReqDTO) {
        // 数据校验
        if(StringUtils.isEmpty(registerReqDTO.getVerifyCode())){
            throw new BadRequestException("验证码错误，请重新获取");
        }
        //远程调用publics服务校验验证码是否正确
        boolean verifyResult = smsCodeApi.verify(registerReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, registerReqDTO.getVerifyCode()).getIsSuccess();
        if(!verifyResult) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        // 注册
        String encode = passwordEncoder.encode(registerReqDTO.getPassword());
        serveProviderService.add(registerReqDTO.getPhone(), UserType.INSTITUTION, encode);

    }
}
